package com.grandtech.insurance.service.impl;


import com.grandtech.insurance.common.bean.CarriesFeature;
import com.grandtech.insurance.common.bean.FeatureSet;
import com.grandtech.insurance.common.db.jdbc.pg.FeatureUtil;
import com.grandtech.insurance.common.utils.ThreadPoolUtil;
import com.grandtech.insurance.common.utils.trans.SimpleList;
import com.grandtech.insurance.common.utils.trans.SimpleMap;
import com.grandtech.insurance.common.utils.trans.Transformation;
import com.grandtech.insurance.service.app.IFeatureService;
import com.grandtech.insurance.service.app.ITileService;
import com.grandtech.insurance.service.app.IVectorTileService;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Geometry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VectorTileService implements IVectorTileService {

    final List<String> BASE_DB_TABLE = new SimpleList<>().push("dt_zzdk");

    //uuid没用起来
    //final String[] MULTI_PK = new String[]{"uuid", "dkbm", "bxgsdm"};
    //final String[] MULTI_PK = new String[]{"dkbm", "uuid"};
    final String[] MULTI_PK = new String[]{"objectid"};

    @Autowired
    IFeatureService iFeatureService;

    @Autowired
    ITileService iTileService;

    @Override
    public void tileFeatures(CarriesFeature carriesFeature, Boolean sync) {
        if (sync == null) sync = true;
        if (sync) {
            tileFeatures(carriesFeature);
        } else {
            ThreadPoolUtil.execute(new Runnable() {
                @Override
                public void run() {
                    tileFeatures(carriesFeature);
                }
            });
        }
    }

    @Override
    public void tileFeatures(CarriesFeature carriesFeature) {
        long start = System.currentTimeMillis();
        SimpleMap<String, Feature> features = carriesFeature.obtainFeatures();
        Feature feature;
        Map map = new HashMap();
        for (String tableName : features.keySet()) {
            feature = features.get(tableName);
            if (feature != null) {
                iTileService.tileByFeature(tableName, feature, map);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("切片执行时间:" + (end - start) + "[ms]");
    }

    @Override
    public CarriesFeature saveAndTileFeature(CarriesFeature carriesFeature) {
        long start = System.currentTimeMillis();
        SimpleMap<String, Feature> features = carriesFeature.obtainFeatures();
        Feature feature;
        CarriesFeature res = CarriesFeature.create();
        for (String tableName : features.keySet()) {
            feature = features.get(tableName);
            feature = FeatureUtil.featureInsertBeforeTile(feature);
            iFeatureService.insertOrUpdate(tableName, feature);
            if (feature != null) {
                iTileService.tileByFeature(tableName, feature);
                res.addFeature(tableName, feature);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("切片执行时间:" + (end - start) + "[ms]");
        return res;
    }

    @Override
    public void saveAndTileFeature(FeatureSet featureSet) {
        List<Feature> features = featureSet.features();

        for (Feature feature : features
        ) {
            Feature feature1 = FeatureUtil.featureInsertBeforeTile(feature);
            Map<String, String> condition = new HashMap<>(featureSet.getKeyWords().length);
            for (String key:featureSet.getKeyWords()
            ) {
                condition.put(key,feature.getStringProperty(key));
            }
            iFeatureService.insertOrUpdate(featureSet.getSourceId(), feature1, condition);
            if (feature != null) {
                iTileService.tileByFeature(featureSet.getSourceId(), feature);
            }
        }

    }

    @Override
    public CarriesFeature delAndTileFeature(CarriesFeature carriesFeature) {
        long start = System.currentTimeMillis();
        SimpleMap<String, Feature> features = carriesFeature.obtainFeatures();
        Feature feature;
        CarriesFeature res = CarriesFeature.create();
        for (String tableName : features.keySet()) {
            feature = features.get(tableName);
            feature = FeatureUtil.featureDelBeforeTile(feature);
            boolean flag = iFeatureService.update(tableName, feature, MULTI_PK);
            if (flag) iTileService.tileByFeature(tableName, feature);
            res.addFeature(tableName, feature);
        }
        long end = System.currentTimeMillis();
        System.out.println("切片执行时间:" + (end - start) + "[ms]");
        return res;
    }

    @Override
    public CarriesFeature editAndTileFeature(CarriesFeature carriesFeature) {
        long start = System.currentTimeMillis();
        SimpleMap<String, Feature> features = carriesFeature.obtainFeatures();
        Feature feature;
        CarriesFeature res = CarriesFeature.create();
        for (String tableName : features.keySet()) {
            feature = features.get(tableName);
            feature = FeatureUtil.featureUpDateBeforeTile(feature);
            boolean flag = iFeatureService.update(tableName, feature, MULTI_PK);
            if (flag) iTileService.tileByFeature(tableName, feature);
            res.addFeature(tableName, feature);
        }
        long end = System.currentTimeMillis();
        System.out.println("切片执行时间:" + (end - start) + "[ms]");
        return res;
    }

    @Override
    public CarriesFeature copyAndTileFeature(CarriesFeature carriesFeature) {
        long start = System.currentTimeMillis();
        SimpleMap<String, Object> carriesExtendItem;
        String fromTableName, toTableName;
        Feature fromFeature, toFeature;
        SimpleMap fromAttribute, toAttribute;
        SimpleMap<String, Object> carriesExtend = carriesFeature.getCarriesExtend();
        CarriesFeature res = CarriesFeature.create();
        for (String key : carriesExtend.keySet()) {
            carriesExtendItem = SimpleMap.fromJson((String) carriesExtend.get(key));
            fromTableName = (String) carriesExtendItem.get("fromTableName");
            toTableName = (String) carriesExtendItem.get("toTableName");
            fromFeature = Feature.fromJson((String) carriesExtendItem.get("fromFeature"));
            fromAttribute = SimpleMap.fromJson((String) carriesExtendItem.get("fromAttribute"));
            toAttribute = SimpleMap.fromJson((String) carriesExtendItem.get("toAttribute"));
            fromFeature = iFeatureService.getFeature(fromTableName, fromFeature);
            toFeature = Feature.fromGeometry(fromFeature.geometry());
            toFeature = FeatureUtil.featureCopy(fromFeature, toFeature, fromAttribute, toAttribute);
            toFeature = FeatureUtil.featureInsertBeforeTile(toFeature);
            boolean flag = iFeatureService.insert(toTableName, toFeature);
            if (flag) iTileService.tileByFeature(toTableName, toFeature);
            res.addFeature(toTableName, toFeature);
        }
        long end = System.currentTimeMillis();
        System.out.println("切片执行时间:" + (end - start) + "[ms]");
        return res;
    }

    @Override
    public CarriesFeature diffAndTileFeature(CarriesFeature carriesFeature) {
        long start = System.currentTimeMillis();
        Feature feature;
        Geometry geometry;
        Geometry otherGeometry;
        Geometry boxResultGeometry;
        com.vividsolutions.jts.geom.Geometry jtsGeometry;
        com.vividsolutions.jts.geom.Geometry jtsOtherGeometry;
        com.vividsolutions.jts.geom.Geometry jtsResultGeometry;
        CarriesFeature res = CarriesFeature.create();
        SimpleMap<String, Feature> features = carriesFeature.obtainFeatures();
        for (String tableName : features.keySet()) {
            feature = features.get(tableName);
            otherGeometry = feature.geometry();
            feature = iFeatureService.getFeature(tableName, feature);
            geometry = feature.geometry();
            jtsGeometry = Transformation.geoJson2JstGeometry(geometry);
            jtsOtherGeometry = Transformation.geoJson2JstGeometry(otherGeometry);
            jtsResultGeometry = jtsGeometry.difference(jtsOtherGeometry);
            Double area = Transformation._84JtsGeometry_2_2000(jtsResultGeometry);
            boxResultGeometry = (Geometry) Transformation.jstGeometry2GeoJson(jtsResultGeometry);
            Feature resFeature = Feature.fromGeometry(boxResultGeometry, feature.properties());
            resFeature.addNumberProperty("mj", Double.parseDouble(String.format("%.2f", area)));

            resFeature = FeatureUtil.featureUpDateBeforeTile(resFeature);
            boolean flag = iFeatureService.update(tableName, resFeature, MULTI_PK);
            if (flag) iTileService.tileByFeature(tableName, resFeature);

            res.addFeature(tableName, resFeature);
        }
        long end = System.currentTimeMillis();
        System.out.println("切片执行时间:" + (end - start) + "[ms]");
        return res;
    }
}
